package com.mohe.dangjian.utils;

import java.util.Calendar;
import java.util.Date;

public class DateStrUtil {
	/**
	 * 根据年龄计算最早出生日期
	 * @param age
	 * @return
	 */
	public static String ageToFirstBirth(Integer age){
		if(age == null){
			return null;
		}
		
		Calendar calendar = Calendar.getInstance();
		String thisYear = String.valueOf(calendar.get(Calendar.YEAR));
		String thisMonth = String.valueOf(calendar.get(Calendar.MONTH));
		String thisDay = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		
		int birthYear = Integer.parseInt(thisYear) - age;
		birthYear--;
		int birthMonth = Integer.parseInt(thisMonth) + 1;
		int birthDay = Integer.parseInt(thisDay) + 1;
		String birthMonthStr = String.valueOf(birthMonth);
		String birthDayStr = String.valueOf(birthDay);
		
		if(birthMonth < 10){
			birthMonthStr = "0" + birthMonthStr;
		}
		
		if(birthDay < 10){
			birthDayStr = "0" + birthDayStr;
		}
		
		String birth = birthYear + "-" + birthMonthStr +  "-" + birthDayStr;
		
		return birth;
	}
	
	/**
	 * 根据年龄计算最晚出生日期
	 * @param age
	 * @return
	 */
	public static String ageToLastBirth(Integer age){
		if(age == null){
			return null;
		}
		
		Calendar calendar = Calendar.getInstance();
		String thisYear = String.valueOf(calendar.get(Calendar.YEAR));
		String thisMonth = String.valueOf(calendar.get(Calendar.MONTH));
		String thisDay = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		
		int birthYear = Integer.parseInt(thisYear) - age;
		int birthMonth = Integer.parseInt(thisMonth) + 1;
		int birthDay = Integer.parseInt(thisDay);
		String birthMonthStr = String.valueOf(birthMonth);
		String birthDayStr = String.valueOf(birthDay);
		
		if(birthMonth < 10){
			birthMonthStr = "0" + birthMonthStr;
		}
		
		if(birthDay < 10){
			birthDayStr = "0" + birthDayStr;
		}
		
		String birth = birthYear + "-" + birthMonthStr +  "-" + birthDayStr;
		
		return birth;
	}
	
	/**
	 * 根据出生日期计算年龄
	 * @param birthDay
	 * @return
	 */
	public static int birthToAge(Date birthDay){
		//获取当前系统时间
        Calendar cal = Calendar.getInstance(); 
        //如果出生日期大于当前时间，则抛出异常
        if (cal.before(birthDay)) { 
            throw new IllegalArgumentException( 
                "The birthDay is before Now.It's unbelievable!"); 
        } 
        //取出系统当前时间的年、月、日部分
        int yearNow = cal.get(Calendar.YEAR); 
        int monthNow = cal.get(Calendar.MONTH); 
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); 
         
        //将日期设置为出生日期
        cal.setTime(birthDay); 
        //取出出生日期的年、月、日部分  
        int yearBirth = cal.get(Calendar.YEAR); 
        int monthBirth = cal.get(Calendar.MONTH); 
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); 
        //当前年份与出生年份相减，初步计算年龄
        int age = yearNow - yearBirth; 
        //当前月份与出生日期的月份相比，如果月份小于出生月份，则年龄上减1，表示不满多少周岁
        if (monthNow <= monthBirth) { 
            //如果月份相等，在比较日期，如果当前日，小于出生日，也减1，表示不满多少周岁
            if (monthNow == monthBirth) { 
                if (dayOfMonthNow < dayOfMonthBirth) {
                	age--; 	
                }
            }else{ 
                age--; 
            } 
        } 
        return age; 
	}
	
	/**
	 * 求d1-d2相差的年数，年数相差小于一年返回0，否则返回相差年数
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static int subForYear(Date d1, Date d2){
		if(null == d1 || d2 == null){
			return 0;
		}
		
		Calendar cal = Calendar.getInstance(); 
		
		// 日期设置为d1
		cal.setTime(d1);
		// 获取年、月、日
		int year1 = cal.get(Calendar.YEAR);
		int month1 = cal.get(Calendar.MONTH);
		int dayOfMonth1 = cal.get(Calendar.DAY_OF_MONTH);
		
		// 日期设置为d1
		cal.setTime(d2);
		// 获取年、月、日
		int year2 = cal.get(Calendar.YEAR);
		int month2 = cal.get(Calendar.MONTH);
		int dayOfMonth2 = cal.get(Calendar.DAY_OF_MONTH);
		
		// 求年、月、日的差值
		int yearDiff = year1 - year2;
		int monthDiff = month1 - month2;
		int dayOfMonthDiff = dayOfMonth1 - dayOfMonth2;
		int diff = 0;
		// 大大大/大大小/大小大/大小小/小大大/小大小/小小大/小小小
		if(yearDiff > 0 && (monthDiff > 0 || (monthDiff == 0 && dayOfMonthDiff >= 0))){
			diff = yearDiff;
		}else{
			diff = 0;
		}
		
		return diff;
	}
	
	/**
	 * 判断d1是否比d2正好大一年
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean isDiffOneYear(Date d1, Date d2){
		if(null == d1 || d2 == null){
			return false;
		}
		
		Calendar cal = Calendar.getInstance(); 
		
		// 日期设置为d1
		cal.setTime(d1);
		// 获取年、月、日
		int year1 = cal.get(Calendar.YEAR);
		int month1 = cal.get(Calendar.MONTH);
		int dayOfMonth1 = cal.get(Calendar.DAY_OF_MONTH);
		
		// 日期设置为d1
		cal.setTime(d2);
		// 获取年、月、日
		int year2 = cal.get(Calendar.YEAR);
		int month2 = cal.get(Calendar.MONTH);
		int dayOfMonth2 = cal.get(Calendar.DAY_OF_MONTH);
		
		// 求年、月、日的差值
		int yearDiff = year1 - year2;
		int monthDiff = month1 - month2;
		int dayOfMonthDiff = dayOfMonth1 - dayOfMonth2;
		// 判断是否正好相差一年
		if(yearDiff == 1 && monthDiff == 0 && dayOfMonthDiff == 0){
			return true;
		}
		
		return false;
	}
}
